<template>
	<div class="login">
		<div class="form-box">
			<h2>Login</h2>
			<a-form
					ref="formRef"
					:label-col="{ span: 6 }"
					:model="formState"
					:rules="rules"
					:wrapper-col="{ span: 18 }"
					autocomplete="off"
					@finish="onFinish"
					@finishFailed="onFinishFailed"
			>
				<a-form-item label="用户名" name="username">
					<a-input v-model:value="formState.username"/>
				</a-form-item>
				
				<a-form-item label="密码" name="password">
					<a-input-password v-model:value="formState.password"/>
				</a-form-item>
				
				<a-form-item :wrapper-col="{ offset: 6, span: 16 }">
					<a-button html-type="submit" type="primary">登陆</a-button>
				</a-form-item>
			</a-form>
		</div>
	</div>
</template>
<script setup>
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import { useUserInfoStore } from '@/store/userInfo.js'
import { useRouterStore } from '@/store/router.js'

const { changeUserInfo, loginHandel } = useUserInfoStore()
const { changeFirstRouter } = useRouterStore()

const router = useRouter()
const formRef = ref()
const formState = reactive({
	username: 'admin',
	password: '123456'
})
const rules = {
	username: [
		{
			required: true,
			message: 'Please input your username!',
			trigger: 'blur'
		}
	],
	password: [
		{
			required: true,
			message: 'Please input your password!',
			trigger: 'blur'
		}
	]
}

// 表单验证验证通过
const onFinish = (values) => {
	if (values.username && values.password) {
		handleLogin()
	} else {
		message.error('请输入')
	}
}
// 表单验证验证失败
const onFinishFailed = (errorInfo) => {
	console.log('Failed:', errorInfo)
}

const handleLogin = async () => {
	await loginHandel(formState)
}
</script>

<style lang="scss" scoped>
.login {
	.form-box {
		width: 500px;
		margin: 0 auto;
		position: fixed;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
		
		h2 {
			text-align: center;
		}
	}
}
</style>
